package org.dromara.third.convert.hoistingWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.HoistingWork;
import org.dromara.system.mapper.HoistingWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.hoistingWork.dto.HoistingWorkDTO;
import org.dromara.third.convert.hoistingWork.validator.HoistingWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertHoistingWorkController {

    @Autowired
    private HoistingWorkMapper hoistingWorkMapper;

    @PostMapping("/hoistingWork")
    @Transactional
    public R<Void> addHoistingWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        HoistingWorkDTO hoistingWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            hoistingWorkDTO = JSONObject.parseObject(decrypt, HoistingWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, hoistingWorkDTO, new HoistingWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        HoistingWork hoistingWork = assembleHoistingWorkData(orgCode, hoistingWorkDTO);
        int count = hoistingWorkMapper.countWithOutDelFlag(hoistingWork.getId());
        if (count == 0) {
            hoistingWorkMapper.insert(hoistingWork);
        } else {
            hoistingWorkMapper.updateWithOutDelFlag(hoistingWork);
        }
        return R.ok();
    }

    private HoistingWork assembleHoistingWorkData(String orgCode, HoistingWorkDTO dto) {
        HoistingWork hoistingWork = new HoistingWork();
        try {
            hoistingWork.setId(dto.getZyid());
            hoistingWork.setZyzbh(dto.getZyzbh());
            hoistingWork.setSqdw(dto.getSqdw());
            hoistingWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            hoistingWork.setSqr(dto.getSqr());
            hoistingWork.setZynr(dto.getZynr());
            hoistingWork.setZywz(dto.getZywz());
            hoistingWork.setDjmc(dto.getDjmc());
            hoistingWork.setDwzl(dto.getDwzl());
            hoistingWork.setZyjb(dto.getZyjb());
            hoistingWork.setZyryxx(dto.getZyryxx());
            hoistingWork.setSsr(dto.getSsr());
            hoistingWork.setDzzhr(dto.getDzzhr());
            hoistingWork.setZydwlx(dto.getZydwlx());
            hoistingWork.setZydwmc(dto.getZydwmc());
            hoistingWork.setZyfzr(dto.getZyfzr());
            hoistingWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            hoistingWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            hoistingWork.setSfsjqttszy(dto.getSfsjqttszy());
            hoistingWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            hoistingWork.setFxbs(dto.getFxbs());
            hoistingWork.setAqjdr(dto.getAqjdr());
            hoistingWork.setJsjdr(dto.getJsjdr());
            hoistingWork.setJhr(dto.getJhr());
            hoistingWork.setDqzt(dto.getDqzt());
            hoistingWork.setLongitude(dto.getLongitude());
            hoistingWork.setLatitude(dto.getLatitude());
            hoistingWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            hoistingWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            hoistingWork.setSpgbbm(dto.getSpgbbm());
            hoistingWork.setFhyp(dto.getFhyp());
            hoistingWork.setAttachment(dto.getAttachment());
            hoistingWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            hoistingWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            hoistingWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            hoistingWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            hoistingWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            hoistingWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            hoistingWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装吊装作业信息异常");
        }
        return hoistingWork;
    }
}
